﻿namespace TanzimSaqib.NoBrainer.Sample.Controllers
{
    using DataAccess;

    public interface IPostController
    {
        Post Create(Post post);
        Post CreatePostLoad(Post post);
        Post View(Post post);
    }

    public class PostController : ControllerBase, IPostController
    {
        private readonly ISingleRepository _Repository;

        public PostController()
        {
            if(_Repository == default(ISingleRepository))
            {
                _Repository = new SingleRepository();
            }
        }

        public PostController(ISingleRepository repository)
        {
            _Repository = repository;
        }

        public Post Create(Post post)
        {
            if (string.IsNullOrEmpty(post.Title))
                post.CmsErrorMessages["Title"] = true;

            if (string.IsNullOrEmpty(post.Body))
                post.CmsErrorMessages["Body"] = true;

            if (string.IsNullOrEmpty(post.TagList))
                post.CmsErrorMessages["TagList"] = true;

            if (!string.IsNullOrEmpty(post.Title) && !string.IsNullOrEmpty(post.Body) && !string.IsNullOrEmpty(post.TagList))
            {
                var tagList = string.Empty;
                var newTags = _Repository.CreateTags(post);
                if(newTags != default(Post))
                {
                    tagList = newTags.TagList;   
                }

                if (!string.IsNullOrWhiteSpace(tagList))
                    post.TagList = tagList.Remove(tagList.Length - 1, 1);
                
                var newPost = _Repository.CreatePost(post);

                if(newPost != default(Post))
                {
                    string.Format("Post created with ID={0}.", newPost.ID).Trace();   
                }

                // Mark that the Model is good to go. The operation was completed successfully.
                post.IsValid = true;
            }

            return post;
        }

        public Post CreatePostLoad(Post post)
        {
            post.Status = 1;
            return post;
        }

        public Post View(Post post)
        {
            var existingPost = _Repository.GetPost(post);

            if(existingPost != default(Post))
            {
                if(existingPost.Status != (int)Post.StatusEnum.Deleted)
                {
                    var postForTags = _Repository.GetTagList(post);
                    if(postForTags != default(Post))
                    {
                        existingPost.TagList = postForTags.TagList;    
                    }

                    var user = _Repository.GetUser(post.UserId);
                    if(user != default(aspnet_User))
                    {
                        existingPost.UserName = user.UserName;                        
                    }

                    post = existingPost;
                    post.IsValid = true;
                }
                else
                {
                    post.ErrorMessage = existingPost.UserId == post.UserId ? 
                        "The post is in deleted status. Please edit and it to Draft first." : 
                        "You are not allow to see this post.";
                }
            }
            else
            {
                post.ErrorMessage = "Post doesn't exist.";
            }

            return post;
        }
    }
}
